package com.wzq.animal.common.exception;

import com.wzq.animal.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 处理自定义的业务异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = GlobalException.class)
    @ResponseBody
    public Result globalExceptionHandler(GlobalException e){
        logger.error("发生业务异常！原因是：{}",e.getStatusCode().getMsg());
        return Result.error(e.getStatusCode().getCode(),e.getStatusCode().getMsg(),e.getObj());
    }


    /**
     * 处理其他异常
     * @param e
     * @return
     */
    @ExceptionHandler(value =Exception.class)
    @ResponseBody
    public Result exceptionHandler(Exception e){
        logger.error("未知异常！原因是:",e);
        return Result.error(StatusCode.ERROR.getCode(),StatusCode.ERROR.getMsg());
    }

}
